<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/6
 * Time: 下午2:00
 */

namespace App\Admin\Controllers;
use App\Busi\Traits\LevelTrait;
use App\Models\Dist;

use App\Models\Level;
use App\Models\SecQualifyCategory;
use App\Models\StaffFee;
use App\Models\SupQualifyCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;

use App\Http\Controllers\Controller;
use App\Models\Quota;
use App\Busi\Traits\CategoryTrait;
use App\Busi\Traits\DistTrait;
class QuotaController extends Controller
{
    use ModelForm,CategoryTrait,DistTrait,LevelTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('资质询价列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增资质询价');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑资质询价');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(Quota::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $obj = $this;
            $grid->subcategory_id('分类名称')->display(function ($category_id) use($obj){
                return $obj->getFullCategoryNameByID($category_id);
            });
            $grid->level("级别")->display(function ($level) use($obj){
                $name = $obj->getLevelNameByLevelId($level);
//                switch ($level){
//                    case 1 :$name = "一级";break;
//                    case 2 :$name = "二级";break;
//                    case 3 :$name = "三级";break;
//                    case 4 :$name = "A级";break;
//                    case 5 :$name = "B级";break;
//                    case 6 :$name = "C级";break;
//                    case 7 :$name = "甲级";break;
//                    case 8 :$name = "乙级";break;
//                    case 9 :$name = "丙级";break;
//                    case 10 :$name = "无分级";break;
//                    default:$name = "无分级";break;
//                }
                return $name;
            });
//            $grid->province_id("所属省份")->display(function ($province_id) use($obj){
//                return $province_name = $obj->getDistNamePid($province_id);
//
//            });
            $grid->city_id("所属市区")->display(function ($city_id) use($obj){
                return  $obj->getDistNamePid($city_id);
            });
            $grid->created_at('创建时间');
            $grid->flag('是否启用')->display(function($flag){
                if( $flag==1 ) return '启用';
                return '禁用';
            });

            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(Quota::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('supcategory_id',"一级类目")->options(SupQualifyCategory::pluck("category_name","id"))->load('subcategory_id', '/api/admin_subcategory');
            $form->select("subcategory_id","二级类目")->options(function (){
                $supcategory_id = $this->supcategory_id;
                return SecQualifyCategory::where("parent_cate_id",$supcategory_id)->pluck('category_name','id');
            });
//            $form->select('province_id',"省")->options(Dist::where("parent_id",-1)->pluck("dist_name","id"))->load('city_id', '/api/city');
//            $form->select("city_id","市")->options(function (){
//                $province_id = $this->province_id;
//                return Dist::where("parent_id",$province_id)->pluck('dist_name','id');
//            });

//            $form->select("level","等级")->options([1=>'一级',2=>"二级",3=>"三级",4=>"A级",5=>"B级",6=>"C级",7=>"甲级",8=>"乙级",9=>"丙级",10=>"无等级"]);
            $form->select("level","等级")->options(Level::pluck("name","id"));
//            $form->select('province_id',"省")->options(Dist::where("parent_id",0)->pluck("dist_name","id"));
            $obj = $this;
            $form->multipleSelect('staffitems','费用单项')->options(function () use($obj){
                $data = StaffFee::all();
                $items = [];
                foreach ($data as $item){
                    $item['fee_min_price'] = empty($item['fee_min_price']) ? 0:$item['fee_min_price'];
                    $item['fee_max_price'] = empty($item['fee_max_price']) ? 0:$item['fee_max_price'];
                    $items[$item['id']] =
                        "省:".$obj->getDistNamePid($item['province_id'])."-市:".$obj->getDistNamePid($item['city_id']).
                        "-资质:".$obj->getFullCategoryNameByID($item['sub_category_id'])."-等级:".$obj->getLevelNameByLevelId($item['level_id'])."-类别:".$item["fee_name"]."-名称:".$item["fee_alias"]."-个数:".$item["fee_num"]."-单价:".$item['fee_price'];
                }
                return $items;
            });
            $form->text("desc", "描述");
            $form->editor('standrad','资质标准');
            $form->editor('scope','办理范围');
            $form->editor('material','所需资料');
//            $form->text('process','办理流程');
//            $form->editor('material','所需材料');

            $form->text('show_order', '展示顺序');
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是']);
        });
    }

}